﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DarwinIsDead.Weapons {
    public class Musket : Weapon {
        private Color RELOAD_COLOR = new Color(255, 255, 0);
        private Vector2 RELOAD_POSITION = new Vector2(0f, 40f);
        private const int RELOAD_WIDTH = 100;
        private const int RELOAD_HEIGHT = 24;
        private const int BULLET_DAMAGE = 6;

        private int currentBullet;

        private bool loaded;
        private bool pressed;
        private float reloadTime;
        private float reloadCounter;

        private float spread;
        private Random rng;

        // Sound effects
        private SoundEffect fireSound;
        private SoundEffect reloadSound;

        // Reload bar
        private Texture2D reloadBar;
        private Rectangle reloadSubrect;

        public Musket(World world, bool friendly)
            : base(world,
                world.Game.Content.Load<Texture2D>("Sprites\\Weapons\\sprMusket"),
                new Vector2(8f, 16f), 80f, friendly){
            for (int i = 0; i < 6; i++) {
                Bullet bullet = new Bullets.Linear(World, BULLET_DAMAGE, 800f, 300f, friendly);
                Bullets.Add(bullet);
                World.AddEntity(bullet);
            }

            reloadTime = 1.5f;
            reloadCounter = 0f;

            loaded = true;
            pressed = false;

            rng = new Random();
            spread = (float)Math.PI / 6f;

            fireSound = World.Game.Content.Load<SoundEffect>("Sounds\\sndMusketFire");

            reloadBar = new Texture2D(world.Game.GraphicsDevice, RELOAD_WIDTH, RELOAD_HEIGHT);
            Color[] data = new Color[RELOAD_WIDTH * RELOAD_HEIGHT];
            for (int i = 0; i < data.Length; i++) {
                data[i] = RELOAD_COLOR;
            }

            reloadBar.SetData(data);

            reloadSubrect = new Rectangle(0, 0, 0, RELOAD_HEIGHT);
        }

        public void Dispose() {
            foreach (Bullet bullet in Bullets) {
                World.RemoveEntity(bullet);
            }
        }

        private void Fire() {
            if (loaded) {
                fireSound.Play();
                foreach (Bullet bullet in Bullets) {
                    bullet.FireAngle = Direction + ((float)rng.NextDouble() - 0.5f) * spread;
                    bullet.Position = new Vector2(
                        Position.X + BarrelLength * (float)Math.Cos(Direction),
                        Position.Y + BarrelLength * (float)Math.Sin(Direction));
                    bullet.Active = true;
                }
                loaded = false;
                reloadCounter = reloadTime;
            }
        }

        public override void InternalUpdate(GameTime gameTime) {
            MouseState mouseState = Mouse.GetState();

            float dt = gameTime.ElapsedGameTime.Milliseconds / 1000f;

            if (reloadCounter > 0f) {
                reloadCounter -= dt;
            } else {
                loaded = true;

                if (!pressed && mouseState.LeftButton == ButtonState.Pressed) {
                    Fire();
                    pressed = true;
                } else if (mouseState.LeftButton == ButtonState.Released) {
                    pressed = false;
                }
            }

            reloadSubrect.Width = (int)(RELOAD_WIDTH * ((reloadTime - reloadCounter) / reloadTime));
        }

        public override void DrawUI(SpriteBatch spriteBatch) {
            spriteBatch.Draw(reloadBar, RELOAD_POSITION, reloadSubrect, Color.White);
        }
    }
}